package com.esd.db.tool;


import java.io.IOException;
import java.security.GeneralSecurityException;
import java.util.Properties;

import javax.mail.Authenticator;
import javax.mail.Message.RecipientType;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

import org.springframework.core.io.support.PropertiesLoaderUtils;

import com.sun.mail.util.MailSSLSocketFactory;



/**
 * 平台剩余任务数预警邮件发送
 * @author sunbo
 */
public class TaskMovedToWorker939MailSender {
	public static String userName;   //发件人用户名
	public static String password;   //发件人密码
	public static String taskRecycleAddressees;    //预警二级 收件人  多个
	
	public TaskMovedToWorker939MailSender(){
		
	}
	
	/**
	 * 发送邮件
	 * @param content  发送邮件内容
	 * @throws MessagingException
	 * @throws GeneralSecurityException
	 */
    public void sendMail(String content, String title) throws MessagingException, GeneralSecurityException{
    	this.getMailParam("application.properties");
        // 配置发送邮件的环境属性
        final Properties props = new Properties();
        /*
         * 可用的属性： mail.store.protocol / mail.transport.protocol / mail.host /
         * mail.user / mail.from
         */
        // 表示SMTP发送邮件，需要进行身份验证
        props.put("mail.smtp.host", "smtp.qq.com");
        props.put("mail.smtp.auth", "true");
       
        // ssl设置
        MailSSLSocketFactory sf = new MailSSLSocketFactory();  
        sf.setTrustAllHosts(true);  
        props.put("mail.smtp.ssl.enable", "true");  
        props.put("mail.smtp.ssl.socketFactory", sf); 
        
        // 构建授权信息，用于进行SMTP进行身份验证
        Authenticator authenticator = new Authenticator() {
            @Override
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(userName, password);
            }
        };
        // 使用环境属性和授权信息，创建邮件会话
        Session mailSession = Session.getInstance(props, authenticator);
        // 创建邮件消息
        MimeMessage message = new MimeMessage(mailSession);
        // 设置发件人
        InternetAddress form = new InternetAddress(userName);
        message.setFrom(form);

        // 设置收件人(单人)
//        InternetAddress to = new InternetAddress("2944009502@qq.com");
//        message.setRecipient(RecipientType.TO, to);
        
        // 设置收件人(多人)
       
        message.setRecipients(RecipientType.TO, taskRecycleAddressees);
        
        
         
        // 设置抄送
//        InternetAddress cc = new InternetAddress("347453848@qq.com");
//        message.setRecipient(RecipientType.CC, cc);

        // 设置密送，其他的收件人不能看到密送的邮件地址
//        InternetAddress bcc = new InternetAddress("347453848@qq.com");
//        message.setRecipient(RecipientType.CC, bcc);

        // 设置邮件标题
        message.setSubject(title);

        // 设置邮件的内容体
        message.setContent(content, "text/html;charset=UTF-8");

        // 发送邮件
        Transport.send(message);
    
    }
    
    /**
	 * 读取配置文件
	 * @param filename
	 */
	public void getMailParam(String filename) {
		Properties propes = new Properties();
			try {
				propes = PropertiesLoaderUtils.loadAllProperties(filename);
	            userName = propes.getProperty("mail.user");
	            password = propes.getProperty("mail.password");
	            taskRecycleAddressees = propes.getProperty("mail.taskRecycleAddressees");
			} catch (IOException e) {
				System.out.println(e.getMessage());
			}
	}
}